enyo.kind({
	name: "AssessmentUI",
	kind: enyo.FittableRows,
	classes: "onyx hpit-bg enyo-unselectable",
	fit: true,
	components: [
		{name:"header", kind: "hpit.Header"},
		{name: "center", kind:"Scroller", touch: true, fit: true},
		{name: "popup", kind: "onyx.Popup", centered: true, modal: true, floating: true, autoDismiss: false, classes: "popup",components:[
			{name:"popupMessage", content:"", classes: "message"},
			{kind:"onyx.Button", content:"Close", ontap:"hideMsg", classes:"button"}
		]},
		{name:"summaryPopup", kind: "onyx.Popup", modal: true, floating: true, centered:true, autoDismiss: false, classes: "popup", components: [
			{content: "Are you sure to submit the request?", classes:"message", style: "padding-left: 10px"},
			{style: "padding: 10px;" , classes:"clearfix", components: [
				{kind: "Button", content: "OK", style:"float:left; width:50%;", ontap: "okButtonClicked"},
				{kind: "Button", content: "Cancel", style:"float: right; width:50%;", ontap: "cancelButtonClicked"}
			]}
		]},
		{name:"footer", kind: "hpit.Footer", onSubmitLeft:"submitLeft", onBack:"back", onSubmitRight:"submitRight"}
	],
	
	current_view: null,
	
	create: function() {
		this.inherited(arguments);
		this.switchView("home");
	},
	
	setCurrentView: function(inConfig) {
		var inControl = this.$.center;
		inControl.destroyClientControls();
		if (inConfig) {
			inControl.createComponent(inConfig, {owner: inControl.owner});
			inControl.render();
		}
	},
	
	switchView: function(viewName) {
		switch(viewName)
		{
			case "home":
				var p = {kind: "AssessmentUI_Home", parentWnd: this};
				AssessmentUI_Data.current_view = "home";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
			case "create_type":
				var p = {kind: "AssessmentUI_CreateType", parentWnd: this};
				AssessmentUI_Data.current_view = "create_type";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
			case "create_device":
				var p = {kind: "AssessmentUI_CreateDevice", parentWnd: this};
				AssessmentUI_Data.current_view = "create_device";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
			case "create_summary":
				var p = {kind: "AssessmentUI_CreateSummary", parentWnd: this};
				AssessmentUI_Data.current_view = "create_summary";
				AssessmentUI_Data.parentWnd = this;
				this.current_view = p;
				this.setCurrentView(p);
				break;
			case "create_result":
				var p = {kind: "AssessmentUI_CreateResult", parentWnd: this};
				AssessmentUI_Data.current_view = "create_result";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
			case "report_run":
				var p = {kind: "AssessmentUI_ReportRun", parentWnd: this};
				AssessmentUI_Data.current_view = "report_run";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
			case "report_detail":
				var p = {kind: "AssessmentUI_ReportDetail", parentWnd: this};
				AssessmentUI_Data.current_view = "report_detail";
				AssessmentUI_Data.parentWnd = this;
				this.setCurrentView(p);
				break;
		}
	},
	
	submitLeft: function() {
		switch(AssessmentUI_Data.current_view)
		{
			case "home":
				break;
			case "create_type":
				if(AssessmentUI_Data.selected_AnalysisTypePayload == null) {
					AssessmentUI_Data.parentWnd.showMsg("No assessment type is selected");
				}else{
					AssessmentUI_Data.parentWnd.switchView("create_device");
				}
				break;
			case "create_device":
				if(this.getSelectedDeviceCount() == 0) {
					//validation
					AssessmentUI_Data.parentWnd.showMsg("No device is selected");
				} else {
					AssessmentUI_Data.parentWnd.switchView("create_summary");
				}
				break;
			case "create_summary":
				
				AssessmentUI_Data.runImmediality = true;
				if(!this.checkSubscriptionParam()) 
					break;
				//submit
				this.$.summaryPopup.show();
				break;
			case "create_result":
				AssessmentUI_Data.parentWnd.switchView("home");
				break;
			case "report_run":
				AssessmentUI_Data.parentWnd.switchView("report_detail");
				break;
			case "report_detail":
				AssessmentUI_Data.parentWnd.switchView("home");
				break;
			default:
				break;
		}
	},
	
	submitRight: function() {
		switch(AssessmentUI_Data.current_view)
		{
			case "home":
				break;
			case "create_type":
				break;
			case "create_device":
				break;
			case "create_summary":
				AssessmentUI_Data.runImmediality = false;
				if(!this.checkSubscriptionParam()) 
					break;
				this.$.summaryPopup.show();
				break;
			case "create_result":
				break;
			case "report_run":
				break;
			case "report_detail":
				break;
			default:
				break;
		}
	},
	
	back: function() {
		switch(AssessmentUI_Data.current_view)
		{
			case "home":
				break;
			case "create_type":
				AssessmentUI_Data.parentWnd.switchView("home");
				break;
			case "create_device":
				AssessmentUI_Data.parentWnd.switchView("create_type");
				break;
			case "create_summary":
				AssessmentUI_Data.parentWnd.switchView("create_device");
				break;
			case "create_result":
				AssessmentUI_Data.parentWnd.switchView("create_summary");
				break;
			case "report_run":
				AssessmentUI_Data.parentWnd.switchView("home");
				break;
			case "report_detail":
				AssessmentUI_Data.parentWnd.switchView("report_run");
				break;
			default:
				break;
		}
	},
	
	getSelectedDeviceCount: function() {
		var count = 0;
		if(AssessmentUI_Data.payload_Devices == null) {
			return count;
		}
		for(var i = 0; i < AssessmentUI_Data.payload_Devices.length; i++) {
			if(AssessmentUI_Data.payload_Devices.payloadChecked[i]) {
				count ++;
			}
		}
		return count;
	},
	
	okButtonClicked: function() {
		this.$.summaryPopup.hide();
		this.submitRequest();
	},
	
	cancelButtonClicked: function() {
		this.$.summaryPopup.hide();
	},
	checkSubscriptionParam: function() {
		if(AssessmentUI_Data.saveSubscription) {
			AssessmentUI_Data.subScriptionTitle = AssessmentUI_Data.current_page.$.titleInput.getValue();
			if(AssessmentUI_Data.subScriptionTitle.length <= 0) {
				this.showMsg("Please input the subscription title!");
				return false;
			}
			return true;
		} else {
			return true;
		}
	},
	
	submitRequest: function() {
		var objValue1 = new Object;
		
		//Check if there is not recipient
		if(AssessmentUI_Data.notificationRecipients.length <= 0) {
			AssessmentUI_Data.notificationRecipients.push(AssessmentUI_Data.current_user);
		}
		
		objValue1.analysisTypeCode = AssessmentUI_Data.selected_AnalysisTypePayload.analysisTypeCode;	//Type code of type
		
		//Selected language
		objValue1.localCodeList = [];
		for(var i = 0; i < AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales.length; i++) {
			var local = AssessmentUI_Data.selected_AnalysisTypePayload.supportedLocales[i];
			if(local.checked) {
				objValue1.localCodeList.push(local.localeCode);
			}
		}
		
		//Selected device
		objValue1.deviceMoosKeys = [];
		for(var i = 0; i < AssessmentUI_Data.payload_Devices.length; i++) {
			if(AssessmentUI_Data.payload_Devices.payloadChecked[i]) {
				objValue1.deviceMoosKeys.push(AssessmentUI_Data.payload_Devices[i].moosKey);
			}
		}
		
		objValue1.organizationSiteInternalKey = AssessmentUI_Data.selected_CustomerOrgSiteKey;	//Org site id
		
		//notification recipients
		objValue1.notificationRecipientAccountIdentifierList = [];
		for(var i = 0; i < AssessmentUI_Data.notificationRecipients.length; i++) {
			objValue1.notificationRecipientAccountIdentifierList.push(AssessmentUI_Data.notificationRecipients[i]);
		}
		
		objValue1.runImmediately = AssessmentUI_Data.runImmediality;
		objValue1.saveSubscription = AssessmentUI_Data.saveSubscription;
		objValue1.subscriptionTitle = AssessmentUI_Data.subScriptionTitle;
		objValue1.frequencyCode = AssessmentUI_Data.frequencyCode;
		objValue1.frequencyNumber = AssessmentUI_Data.frequencyNum;
		
		
		
		var service = new hpit.WebService();
		service.setUrl(AssessmentUI_Config.getServiceUrl_CreateAnalysisRequest());
		service.setHeaders(AssessmentUI_Config.getCommonServiceHeader());
		service.setParams({value0: "mobile.test"},{value1: enyo.json.stringify(objValue1)});
		service.call(this, "createServiceGot");
	},
	
	createServiceGot: function(inSender, inResponse) {
		this.serviceResponse = inResponse;
		if(this.serviceResponse.statusCode === 200) {
			if(inResponse.payload > 0) {
				AssessmentUI_Data.newRequestID = inResponse.payload;	//store the new request ID
				this.switchView("create_result");
			} else {
				this.showMsg("ERROR: requestID:" + AssessmentUI_Data.newRequestID);
			}
		}
		else {
			this.showMsg("Error response of service (StatusCode: " + inResponse.statusCode + ")");
		}
	},
	
	showMsg: function(msg) {
		this.$.popupMessage.setContent(msg);
		this.$.popup.show();
	},
	
	hideMsg: function() {
		this.$.popup.hide();
	},
	
	showScrim: function() {
		//TODO
	},
	
	hideScrim: function() {
		//TODO
	}
});